/*
 *  txdnet.cn tonydon
 * 
 */
package cn.txdnet.txcms.admin.action;

import cn.txdnet.txweb.mail.EmailSenderTask;
import cn.txdnet.txweb.mail.Result;
import cn.txdnet.txweb.mail.SimpleSender;
import cn.txdnet.txweb.model.ModelUtil;
import cn.txdnet.txweb.model.ModelView;
import cn.txdnet.txweb.utils.Constants;
import cn.txdnet.txweb.utils.Execute;
import cn.txdnet.txweb.utils.Message;
import cn.txdnet.txweb.utils.StringUtil;
import cn.txdnet.txweb.utils.WebUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import javax.servlet.http.HttpServletRequest;
import txdnet.util.DataUtil;
import txdnet.util.FileUtil;
import txdnet.util.Logger;
import txdnet.util.StaticMark;

/**
 *
 * @author txdnet
 */
public class MailerAction {

    public ModelView send_edm(HttpServletRequest req) {
        String name = WebUtil.getNoNull(req, "name", null);
        String subject = WebUtil.getNoNull(req, "subject", null);
        String mailList = WebUtil.getNoNull(req, "mailList", null);
        String content = WebUtil.getNoNull(req, "content", null);
        if (name == null || subject == null || mailList == null || content == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        List<SimpleSender> recMailList = new ArrayList<SimpleSender>();
        String[] mailArray = StringUtil.split(mailList, StaticMark.CHAR_COMMA);
        injectEmailToSenderList(name, subject, content, recMailList, mailArray);
        List<Future<Result>> list = new ArrayList<Future<Result>>(recMailList.size());
        for (SimpleSender s : recMailList) {
            list.add(Execute.run(new EmailSenderTask(s)));
        }
        StringBuilder sb = new StringBuilder();
        String log = String.format("/logs/%s.edm.log", DataUtil.getDate(StaticMark.DTM_YYYYMMDDHHMMSSSSS));
        try{
        for (Future<Result> future : list) {
            Result result = future.get();
            sb.append(String.format("%s,%s%n", result.getMail(), result.getSendFlag()));
        }
        FileUtil.writeStringToFile(WebUtil.WEB_APP_PATH.concat(log), sb.toString());
        }catch(Exception e){
            Logger.error(MailerAction.class, e);
            return ModelUtil.getTextModel(e.toString());
        }
        return ModelUtil.getTextModel(Message.MSG_SUCCESS.concat(", Edm log:").concat(log));
    }

    private void injectEmailToSenderList(String name, String subject, String content,
            List<SimpleSender> recMailList, String... mails) {
        for (String mail : mails) {
            if (StringUtil.isEmpty(mail) || !Constants.RE_EMAIL.matcher(mail).matches()) {
                continue;
            }
            SimpleSender sender = new SimpleSender();
            sender.setSenderName(name);
            sender.setTomail(mail);
            sender.setSubject(subject);
            sender.setIsHtml(true);
            sender.setContent(content.replace("$mail", mail));
            recMailList.add(sender);
        }
    }
}
